/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React from 'react';
// import { Text } from 'react-native';
import { NavigationActions } from 'react-navigation';
import { WingBlank, WhiteSpace, Toast } from 'antd-mobile-rn';
import CardWoucherCard from '../../components/CardWoucherCard';
import PullList from '../../components/PullList';
import BaseView from '../BaseView';
import { getCardVoucherList } from '../../model/cardWoucher';

export default class CardWoucher extends BaseView {
  constructor(props) {
    super(props);
    this.dataSource = [
      {
        id: 1,
        endTime: '2018-1-1',
        price: 20,
        status: '待审核'
      },
      {
        id: 2,
        endTime: '2018-1-1',
        price: 20,
        status: '待审核'
      },
      {
        id: 3,
        endTime: '2018-1-1',
        price: 20,
        status: '待审核'
      },
      {
        id: 4,
        endTime: '2018-1-1',
        price: 20,
        status: '待审核'
      }
    ];
    this.loadMore = this.loadMore.bind(this);
    this.state = {
      nomore: false
    };
  }

  // 生命周期render前
  async componentWillMount() {
    const { navigation } = this.props;
    navigation.setParams({ goback: this.goback.bind(this) });
    const { dataObject } = await getCardVoucherList();
    console.log(dataObject.aaData.length);
  }


  componentDidMount() {
    const { navigation } = this.props;
    super.componentDidMount();
    this.props.navigation.setParams({
      title: '卡券管理',
      rightTxt: '新增',
      rightClick: () => {
        const navigateAction = NavigationActions.navigate({
          routeName: 'CardWoucherForm',
          params: {}
        });
        navigation.dispatch(navigateAction);
      }
    });
    Toast.loading('加载中');
  }

  goback() {
    const { navigation } = this.props;
    const backAction = NavigationActions.back();
    navigation.dispatch(backAction);
  }

  onPullRelease = (resolve) => {
    try {
      setTimeout(() => {
        if (this.pullList) {
          this.pullList.setData([
            {
              id: 1,
              endTime: '2018-1-1',
              price: 20,
              status: '待审核'
            }
          ]);
        }
        if (resolve) resolve();
      }, 2000);
    } catch (err) {
      if (resolve) resolve();
      Log(err);
    }
  };

  loadMore() {
    setTimeout(() => {
      if (this.pullList) {
        this.pullList.addData([
          {
            id: 1,
            endTime: '2018-1-1',
            price: 20,
            status: '待审核'
          },
          {
            id: 1,
            endTime: '2018-1-1',
            price: 20,
            status: '待审核'
          },
          {
            id: 1,
            endTime: '2018-1-1',
            price: 20,
            status: '待审核'
          },
          {
            id: 1,
            endTime: '2018-1-1',
            price: 20,
            status: '待审核'
          },
          {
            id: 1,
            endTime: '2018-1-1',
            price: 20,
            status: '待审核'
          }
        ]);
      }
      Toast.hide();
    }, 10);
  }

  renderRow({ item /* , index, separators */ }) {
    return (
      <WingBlank>
        <WhiteSpace />
        <CardWoucherCard
          endTime={item.endTime}
          price={item.price}
          status={item.status}
          key={item.id}
        />
      </WingBlank>
    );
  }

  render() {
    const { nomore } = this.state;

    return (
      /**
       * @param {boolean} nomore - 是否还有更多数据， ture：没有更多了，false：还有
       * @param {function} initData - 初始数据定义
       * @param {function} onPullRelease - 下拉刷新调用方法
       * @param {function} renderRow - 渲染列
       * @param {function} renderRow - 渲染列
       * @param {function} onEndReached - 加载更多
       */
      <PullList
        style={{ backgroundColor: '#fff' }}
        nomore={nomore}
        initData={(list) => {
          this.pullList = list;
        }}
        onPullRelease={this.onPullRelease}
        renderRow={this.renderRow}
        onEndReached={this.loadMore}
      />
    );
  }
}
